

#include "compress.h"
#include "./../base64/base64.h"
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>
#include <zconf.h>
#include <zlib.h>

CompressData::CompressData()
{
	
}


int CompressData::UnCompress(const std::string & message, char * uncompress_data_buffer, int buffer_length)
{
	std::string decode     = base64_decode(message);
	const Bytef * input    = (const Bytef*)decode.c_str();
	char        * output   = uncompress_data_buffer;
	uLongf output_length   = buffer_length; 
	int result = uncompress((Bytef*)output, &output_length, input, decode.size());
	if (result != Z_OK)
	{
		return EmCompressFail;
	}
	return EmCompressFinish;
}

int CompressData::Compress(const unsigned char * data, int length, std::string & compress_data)
{
	const Bytef * input  = data;
	uLong input_length   = length;
	std::vector<Bytef>  output_vec(length);
	uLongf output_length = length;
	int result = compress(&(*output_vec.begin()), &output_length, input, input_length);
	if (result != Z_OK)
	{
		return EmCompressFail;
	}
	else
	{
		compress_data = base64_encode(std::string((char*)&(*output_vec.begin()), output_length));
		return EmCompressFinish;
	}
}

int CompressData::CRC32(const uint8_t * data, int length)
{
	auto calu = crc32(0, Z_NULL, 0);
	calu = crc32(calu, (const uint8_t *)data, length);
	printf("crc32-result:0x%08lx.\n", calu);
	return 0;
}
